package com.softer.chengxin.project.service.impl;

import com.softer.chengxin.common.domin.*;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.person.dao.PersonDao;
import com.softer.chengxin.project.dao.ProjectDao;
import com.softer.chengxin.project.domin.Project;
import com.softer.chengxin.project.service.ProjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/9/27
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private PersonDao personDao;

    /**
     * 查询项目列表
     *
     * @param id        企业ID
     * @param projectId 项目ID
     * @return 项目列表
     */
    @Override
    public List<Project> queryProjectList(Integer id, String projectId) {
        return projectDao.queryProjectList(id, projectId);
    }

    /**
     * 删除项目
     *
     * @param id 项目ID
     * @return 结果
     */
    @Override
    public CommonResult delete(Integer id) {
        int num = projectDao.delete(id);
        if (num > Integer.parseInt(Common.ZERO)) {
            //删除项目人员
            personDao.deleteProjectPerson(id);
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "删除成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "删除失败");
    }

    /**
     * 提交项目
     *
     * @param id 项目ID
     * @return 结果
     */
    @Override
    public CommonResult submit(Integer id) {
        int num = projectDao.submit(id);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "提交成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "提交失败");
    }

    /**
     * 查询项目信息列表
     *
     * @param page    页面实体
     * @param project 查询实体
     * @return 列表
     */
    @Override
    public ManagerPageResult query(Page page, Project project) {
        ManagerPageResult mr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        String states = "";
        if (Common.ADD.equals(project.getType())) {
            //信息录入页面（查询保存的，审核通过,审核不通过的项目信息）
            states = "0,3";
        } else if (Common.QUERY.equals(project.getType())) {
            //信息查询页面（查询审核中，审核通过项目信息）
            states = "1,2,4";
        }
        int num = projectDao.queryNum(project, states);
        if (num > Integer.parseInt(Common.ZERO)) {
            List<Project> list = projectDao.queryList(page, project, states);
            return setResult(mr, jr, num, list);
        }
        return setResult(mr, jr, Integer.parseInt(Common.ZERO), new ArrayList());
    }

    /**
     * 增加项目信息
     *
     * @param project 项目实体
     * @return 增加结果
     */
    @Override
    public CommonResult add(Project project) {
        if (StringUtils.isBlank(project.getProjectName())) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请添加项目名称");
        }
        if (StringUtils.isBlank(project.getProjectId())) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请添加项目ID");
        }
        Integer id = projectDao.getProjectId(project.getProjectId());
        if (id != null) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "添加项目失败，请稍后重试");
        }
        String projectName = projectDao.checkProjectName(project);
        if (StringUtils.isNotBlank(projectName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "该项目已经添加过");
        }
        int num = projectDao.add(project);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "新增成功", project.getId());
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "新增失败");
    }

    /**
     * 查询具体项目信息
     *
     * @param id 项目ID
     * @return 项目信息
     */
    @Override
    public Project detail(Integer id) {
        Project project = projectDao.detail(id);
        if (project != null) {
            String opinion = projectDao.queryProjectOpinion(id);
            project.setOpinion(opinion);
        }
        return project;
    }

    /**
     * 编辑项目信息
     *
     * @param project 项目实体
     * @return 编辑结果
     */
    @Override
    public CommonResult edit(Project project) {
        int num = projectDao.edit(project);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "修改成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "修改失败");
    }

    /**
     * 获取企业下所有项目
     *
     * @param id 企业ID
     * @return 项目列表
     */
    @Override
    public List<Project> getAllProject(Integer id) {
        return projectDao.getAllProject(id);
    }

    /**
     * 设置返回参数
     *
     * @param mpr  ManagerPageResult
     * @param jr   JsonResult
     * @param num  数量
     * @param list 集合列表
     */
    private ManagerPageResult setResult(ManagerPageResult mpr, JsonResult jr, int num, List list) {
        mpr.setCount(num);
        jr.setDs(list);
        mpr.setJson(jr);
        return mpr;
    }
}
